package ink.xiaobaibai.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import ink.xiaobaibai.annotation.InsidePublicResources;
import ink.xiaobaibai.common.DepartmentVo;
import ink.xiaobaibai.entity.AdminUser;
import ink.xiaobaibai.response.ResponseFormat;
import ink.xiaobaibai.service.IAdminUserService;
import ink.xiaobaibai.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @description: 附加信息接口
 * @author: 小白白
 * @create: 2021-07-18
 **/

@RestController
@RequestMapping("/additional")
@Api(tags = "附加信息接口")
@CrossOrigin
@InsidePublicResources
@Slf4j
public class AdditionalController {

    // TODO mysql索引
    // TODO 管理端的接口全部 @Insider.. 注解;检查各个权限注解+初始化权限库表

    /**
     * 月费机器托管: 后台可以设置时间+生效 客户端设置是否后台可操作
     */

    @Autowired
    private IAdminUserService adminUserService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private IDepartmentService departmentService;

    public static String VERSION_KEY = "userMappingVersionKey";
    private static Random random = new Random();

    @GetMapping("/userName-id-Mapping")
    @ApiOperation("获取用户集合 只有 id+姓名,前端获取后转为map存储")
    public ResponseFormat userNameIdMapping(@RequestParam("version") String outVersion) {
        String version = this.stringRedisTemplate.opsForValue().get(VERSION_KEY);
        //存在且相同
        if (version != null && version.equals(outVersion)) {
            return ResponseFormat.ok("version ok");
        }
        //服务器已经修改,更新一下标识
        if (version == null) {
            version = String.valueOf(random.nextInt(10000));
            this.stringRedisTemplate.opsForValue().set(VERSION_KEY, version);
        }
        QueryWrapper<AdminUser> q1 = new QueryWrapper<>();
        q1.select("id", "user_name", "user_phone");
        List<AdminUser> list = this.adminUserService.list(q1);
        log.info("查了一次用户映射");
        return ResponseFormat.ok(version, list);
    }

    @GetMapping("/list")
    @ApiOperation("获取部门列表")
    public ResponseFormat getDepartmentList() {
        List<AdminUser> agentUserList = this.adminUserService.getAgentUser();
        Map<Integer, List<DepartmentVo.DepUser>> map = new HashMap<>(agentUserList.size());
        for (AdminUser adminUser : agentUserList) {
            map.compute(adminUser.getDepartmentId(), (k, v) -> {
                if (v == null) {
                    List<DepartmentVo.DepUser> list = new ArrayList<>();
                    list.add(new DepartmentVo.DepUser(adminUser.getUserName(), adminUser.getUserPhone()));
                    return list;
                } else {
                    v.add(new DepartmentVo.DepUser(adminUser.getUserName(), adminUser.getUserPhone()));
                    return v;
                }
            });
        }
        List<DepartmentVo> departmentVoList = this.departmentService.list().stream().map(d -> new DepartmentVo(d.getId(),
                d.getDepartmentName(), d.getCreateTime(), map.get(d.getId()))).collect(Collectors.toList());
        return ResponseFormat.success(departmentVoList);
    }

}
